package com.linewow.xhyy.nihaonew.adapter;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.linewow.xhyy.hrxb.DemoHelper;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.entity.NiHaoGeneral;
import com.linewow.xhyy.nihaonew.entity.user.Uinfo;
import com.linewow.xhyy.nihaonew.entity.image.SimpleImagesInfo;
import com.linewow.xhyy.nihaonew.entity.tag.CustomTagsInfo;
import com.linewow.xhyy.nihaonew.entity.topic.FindTagTopicInfo;
import com.linewow.xhyy.nihaonew.inter.OnThumbUp;
import com.linewow.xhyy.nihaonew.model.HttpModel;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;
import com.linewow.xhyy.nihaonew.util.CommonUtil;
import com.linewow.xhyy.nihaonew.view.HeartView;
import com.linewow.xhyy.nihaonew.view.VoiceButton2Red;
import com.squareup.picasso.Picasso;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/9/9.
 * 多小图话题
 */
public class FindTagTopicAdapter extends BaseQuickAdapter<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> {

    private String TAG="FindTagTopicAdapter";
    public FindTagTopicAdapter(List<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> data) {
        super(R.layout.item_tag_detail,data);
    }

    @Override
    protected void convert(final BaseViewHolder baseViewHolder, final FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo> findTagTopicInfo) {
        List<SimpleImagesInfo>simpleImagesInfos=findTagTopicInfo.images;
        Picasso.with(baseViewHolder.getConvertView().getContext()).load(findTagTopicInfo.avatar.replace("m.en1on1.com", "huibenabc.com")).fit().error(R.mipmap.err_avatar).into((ImageView) baseViewHolder.getView(R.id.widget_personal_message_avatar));
        baseViewHolder.setText(R.id.widget_personal_message_nickname,findTagTopicInfo.uinfo.nickname);
        if(findTagTopicInfo.location.equals("null")){
            baseViewHolder.setVisible(R.id.widget_personal_message_address_line,false);
        }else{
            baseViewHolder.setVisible(R.id.widget_personal_message_address_line,true);
            baseViewHolder.setText(R.id.widget_personal_message_address_tv,findTagTopicInfo.location);
        }
        baseViewHolder.setText(R.id.widget_personal_message_time_tv,findTagTopicInfo.time);


        baseViewHolder.setVisible(R.id.item_tag_detail_content,true);
        if(findTagTopicInfo.subject.equals("null")){
            if(findTagTopicInfo.contents.equals("")){
                baseViewHolder.setVisible(R.id.item_tag_detail_content,false);
            }else{
                baseViewHolder.setText(R.id.item_tag_detail_content,findTagTopicInfo.contents);
            }
        }else{
            baseViewHolder.setText(R.id.item_tag_detail_content,findTagTopicInfo.subject);
        }

        baseViewHolder.addOnClickListener(R.id.widget_topic_menu_like_rela);
        baseViewHolder.addOnClickListener(R.id.widget_topic_menu_conversation_rela);
        baseViewHolder.addOnClickListener(R.id.widget_personal_message_avatar);

        VoiceButton2Red voiceButton=baseViewHolder.getView(R.id.item_tag_detail_voice_button);
        if(findTagTopicInfo.audiotime.endsWith("0")){
            voiceButton.setVisibility(View.GONE);
        }else{
            voiceButton.setVisibility(View.VISIBLE);
            voiceButton.setUrlPath(findTagTopicInfo.audiotime,findTagTopicInfo.audiopath);

        }

        baseViewHolder.setText(R.id.widget_topic_menu_like_tv,findTagTopicInfo.upnum);
        baseViewHolder.setText(R.id.widget_topic_menu_topic_tv,findTagTopicInfo.replynum);


        HeartView heartView=baseViewHolder.getView(R.id.widget_topic_menu_like_img);
        if(findTagTopicInfo.zan.equals("1")){
//            baseViewHolder.setImageResource(R.id.widget_topic_menu_like_img,R.mipmap.icon_like_red);
            heartView.staticLike();
        }else{
//            baseViewHolder.setImageResource(R.id.widget_topic_menu_like_img,R.mipmap.icon_like_gray);
            heartView.staticUnLike();
        }

        final int likeNum=Integer.parseInt(findTagTopicInfo.upnum);
        heartView.setOnThumbUp(new OnThumbUp() {
            @Override
            public void like(boolean likeFlag) {
               Log.e("FindTagTopicAdapter","点击了"+likeFlag+"--->"+findTagTopicInfo.tid+"--->"+likeNum);

//                if(noLogin()){
//                    return;
//                }
//                requestZan(likeFlag,findTagTopicInfo);
                if(likeFlag){
                    baseViewHolder.setText(R.id.widget_topic_menu_like_tv,""+(likeNum+1));
                    findTagTopicInfo.setUpnum(""+(likeNum+1));
                    findTagTopicInfo.setZan("1");
                }else{
                    baseViewHolder.setText(R.id.widget_topic_menu_like_tv,""+likeNum);
                    findTagTopicInfo.setUpnum(""+(likeNum));
                    findTagTopicInfo.setZan("0");
                }

            }
        });

        Picasso.with(baseViewHolder.getConvertView().getContext()).load((simpleImagesInfos.get(0).image+ HttpModel.IMAGEBIGSUFFIX).replace("m.en1on1.com", "huibenabc.com")).error(R.mipmap.err_topic).transform(CommonUtil.getTransformation(baseViewHolder.getConvertView().getContext()))
                .into((ImageView) baseViewHolder.getView(R.id.item_tag_detail_img1));
        ImagesHorizontalAdapter adapter=new ImagesHorizontalAdapter(simpleImagesInfos,baseViewHolder.getConvertView().getContext());
        RecyclerView itemTagDetailRecycler=baseViewHolder.getView(R.id.item_tag_detail_recycler);
        LinearLayoutManager manager=new LinearLayoutManager(baseViewHolder.getConvertView().getContext());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        itemTagDetailRecycler.setLayoutManager(manager);
        itemTagDetailRecycler.setItemAnimator(new DefaultItemAnimator());

        itemTagDetailRecycler.setAdapter(adapter);
    }


    public void requestZan(boolean zanFlag,FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>info){
        if(zanFlag){
            NiHaoOkgo.zanInfo(info)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<NiHaoGeneral<String>>() {
                @Override
                public void call(NiHaoGeneral<String> stringNiHaoGeneral) {
                    if (stringNiHaoGeneral.status) {

                    }
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {

                }
            });
        }else{
            NiHaoOkgo.zanInverseInfo(info)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Action1<NiHaoGeneral<String>>() {
                                        @Override
                                        public void call(NiHaoGeneral<String> stringNiHaoGeneral) {
                                        }
                                    }, new Action1<Throwable>() {
                                        @Override
                                        public void call(Throwable throwable) {
                                        }
                                    });

        }
    }

    public boolean noLogin(){
        if(NiHaoApp.flag){
            return  false;
        }else{
            DemoHelper.getInstance().onConnectionConflict();
            return true;
        }
    }

}
